//
//  HWInviteRewardCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/9/15.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWInviteRewardCell: UITableViewCell {
    var bgView:UIView!
    var headImageView:UIImageView!
    var nameLabel:UILabel!
    var lineView:UIView!
    var regTimeTitle:UILabel!
    var regTimeLabel:UILabel!
    var loginTimeTitle:UILabel!
    var loginTimeLabel:UILabel!
    var sendButton:UIButton!
    var rewardTitle:UILabel!
    var reward:UILabel!
    var model:HWInviteRecordModel? {
        didSet {
            nameLabel.text = model?.user_name
            regTimeLabel.text = model?.reg_time
            loginTimeLabel.text = model?.login_time
            headImageView.kf.setImage(with: URL(string: model?.face ?? ""))
            if model?.is_fafang == true {
                sendButton.isHidden = true
                rewardTitle.isHidden = false
                reward.isHidden = false
                reward.text = model?.fafang
            } else {
                sendButton.isHidden = false
                rewardTitle.isHidden = true
                reward.isHidden = true
            }
        }
    }
    
    var sendButtonClick:((_:HWInviteRecordModel)->())?
    
    class func cellWithTableView(tableView:UITableView) -> (HWInviteRewardCell) {
        let reUse:String = "HWInviteRewardCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWInviteRewardCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWInviteRewardCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        bgView = UIView()
        bgView.backgroundColor = .white
        bgView.layer.cornerRadius = 18*widthScale
        contentView.addSubview(bgView)
        
        headImageView = UIImageView()
        headImageView.layer.cornerRadius = 14*widthScale
        headImageView.layer.masksToBounds = true
        bgView.addSubview(headImageView)
        
        nameLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 13*widthScale)
        bgView.addSubview(nameLabel)
        
        lineView = UIView()
        lineView.frame = CGRect(x: 0, y: 0, width: 321*widthScale, height: 1*widthScale)
        bgView.addSubview(lineView)
        lineView.drawDashLine(lineView, strokeColor: UIColor.colorWithHexString(hexString: "#CCCCCC"), lineWidth: 1, lineLength: 1, lineSpacing: 5)
        
        regTimeTitle = HWLabel.createLabel(title: "注册时间", color: "#333333", font: 11*widthScale)
        bgView.addSubview(regTimeTitle)
        
        regTimeLabel = HWLabel.createLabel(title: "", color: "#888888", font: 11*widthScale)
        bgView.addSubview(regTimeLabel)
        
        loginTimeTitle = HWLabel.createLabel(title: "最近登录", color: "#333333", font: 11*widthScale)
        bgView.addSubview(loginTimeTitle)
        
        loginTimeLabel = HWLabel.createLabel(title: "", color: "#888888", font: 11*widthScale)
        bgView.addSubview(loginTimeLabel)
        
        sendButton = UIButton(type: .custom)
        sendButton.setTitle("发放", for: .normal)
        sendButton.titleLabel?.font = .boldSystemFont(ofSize: 13*widthScale)
        sendButton.frame = CGRect(x: 0, y: 0, width: 54*widthScale, height: 30*widthScale)
        sendButton.layer.cornerRadius = 15*widthScale
        sendButton.layer.masksToBounds = true
        sendButton.addTarget(self, action: #selector(sendButtonClick(_:)), for: .touchUpInside)
        bgView.addSubview(sendButton)
        UIColor.gradientColor(view: sendButton, colors: [UIColor.colorWithHexString(hexString: "#FFB60A").cgColor,
                                                         UIColor.colorWithHexString(hexString: "#F66A05").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
        
        rewardTitle = HWLabel.createLabel(title: "奖励", color: "#333333", font: 11*widthScale)
        bgView.addSubview(rewardTitle)
        
        reward = HWLabel.createLabel(title: "", color: "#888888", font: 11*widthScale)
        bgView.addSubview(reward)
    }
    
    func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.right.equalTo(-18*widthScale)
            make.top.equalToSuperview().priority(.high)
            make.bottom.equalTo(-4*widthScale)
        }
        
        headImageView.snp.makeConstraints { make in
            make.left.top.equalTo(9*widthScale)
            make.width.height.equalTo(28*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(5*widthScale)
            make.centerY.equalTo(headImageView)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.equalTo(9*widthScale)
            make.right.equalTo(-9*widthScale)
            make.height.equalTo(1*widthScale)
            make.top.equalTo(headImageView.snp.bottom).offset(9*widthScale)
        }
        
        regTimeTitle.snp.makeConstraints { make in
            make.left.equalTo(9*widthScale)
            make.top.equalTo(lineView.snp.bottom).offset(8*widthScale)
            make.height.equalTo(15*widthScale)
        }
        
        regTimeLabel.snp.makeConstraints { make in
            make.left.equalTo(regTimeTitle)
            make.top.equalTo(regTimeTitle.snp.bottom).offset(2*widthScale)
            make.height.equalTo(15*widthScale)
            make.bottom.equalTo(-8*widthScale)
        }
        
        loginTimeTitle.snp.makeConstraints { make in
            make.left.equalTo(regTimeTitle.snp.right).offset(65*widthScale)
            make.top.equalTo(regTimeTitle)
            make.height.equalTo(15*widthScale)
        }
        
        loginTimeLabel.snp.makeConstraints { make in
            make.left.equalTo(loginTimeTitle)
            make.top.equalTo(loginTimeTitle.snp.bottom).offset(2*widthScale)
            make.height.equalTo(15*widthScale)
        }
        
        sendButton.snp.makeConstraints { make in
            make.right.equalTo(-9*widthScale)
            make.width.equalTo(54*widthScale)
            make.height.equalTo(30*widthScale)
            make.top.equalTo(lineView.snp.bottom).offset(10*widthScale)
        }
        
        rewardTitle.snp.makeConstraints { make in
            make.right.equalTo(-27*widthScale)
            make.top.equalTo(lineView.snp.bottom).offset(8*widthScale)
            make.height.equalTo(15*widthScale)
        }
        
        reward.snp.makeConstraints { make in
            make.centerX.equalTo(rewardTitle)
            make.top.equalTo(rewardTitle.snp.bottom).offset(2*widthScale)
            make.height.equalTo(15*widthScale)
        }
    }
    
    @objc func sendButtonClick(_:UIButton) -> Void {
        self.sendButtonClick?(self.model ?? HWInviteRecordModel())
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
